<?php
namespace app\manage\validate;
use think\Validate;
class UserValidate extends Validate
{
     protected $rule = [
        'username' =>  'require|lizhiliUnique:user',
        'sex' =>  'require',
        'phone'=>'lizhiliUnique:user',
        'email'=>'email|lizhiliUnique:user',

    ];
	 protected $message  =   [
        'username.require' => '用户名必须填写', 
        'sex.require' => '性别必须填写', 
        'username.lizhiliUnique'     => '当前用户名已经存在，不能重复！',
        'phone.lizhiliUnique'     => '当前电话已经存在，不能重复！',
        'email.lizhiliUnique'     => '当前邮箱已经存在，不能重复！',
        'email.email' => '邮箱格式不正确',
    ];
	protected $scene = [
        'edit'  =>  ['password'=>'require|length:6,16'],
    ];
    // 自定义验证规则
    protected function lizhiliUnique($value, $rule, $data=[],$field='',$desc='')
    {
        $where = [['delete_time','=',Null],[$field,'=',$value]];
        $str='\\app\\model\\'.ucfirst($rule).'Model';
        $count = (new $str())->where($where)->count();
        if($count){
            return false;
        }
        return true;
    }
}
